<?php
//@author wangyuhua 2012/3/12
class WebMessageController extends AppController {
	var $name = 'WebMessage';

	var $uses = array (
		'ScoutMsgInfo'
		,'ScoutMailJob'
		,'ScoutMsgReactionInfo'
		,'ThemaInfo'
		,'WebMsgInfo'
		,'Company'
		,'CompanyAccount'
		,'MemberEmailMsg'
		,'NoticeMsgReactionInfo'
	);
	
	function receivemsg($status=null) {
		$member_id = $this->rdAuth->id;
		if (empty($member_id)) {
			$this->redirect(FRONT_SERVER_HTTP.FRONT_SERVER_NAME.'/memberLogin/login/webMessage/receivemsg');
			exit;
		}
		$this->pageTitle='受信メッセージ一覧';
		
		$condition = "(MemberEmailMsg.mailflg='1' or MemberEmailMsg.mail_from='0') and MemberEmailMsg.member_id="
						.Sanitize::sql($member_id)
						." and MemberEmailMsg.sr_deleted is null and MemberEmailMsg.sr_member_deleted is null"
						." and MemberEmailMsg.msg_deleted is null and MemberEmailMsg.msg_member_deleted is null";
		//返回记录总数
		$count=$this->MemberEmailMsg->findCount($condition);
		//状态排序
		if (isset($status)) {
			$scoutMsgInfo = $this->MemberEmailMsg->findAll($condition,null,'MemberEmailMsg.sortstatus',$this->show,$this->page);
			$this->setPaging('ajax','/webMessage/receivemsg/status',$count,$this->show,null,$this->show);
		} else {
			$scoutMsgInfo = $this->MemberEmailMsg->findAll($condition,null,null,$this->show,$this->page);
			$this->setPaging('ajax','/webMessage/receivemsg',$count,$this->show,null,$this->show);
		}
		for ($i=0;$i<count($scoutMsgInfo);$i++) {
			$scoutMsgInfo[$i]['MemberEmailMsg']['received'] = date("Y/m/d H:i",strtotime($scoutMsgInfo[$i]['MemberEmailMsg']['received']));
		}
		$this->pageInfo($count);
		$this->set("scoutMsgInfo",$scoutMsgInfo);
// 		$this->set("page",$this->page);
 		$this->render('receivemsg');
	}
	
	function replymsg($status=null) {
		$member_id = $this->rdAuth->id;
		if (empty($member_id)) {
			$this->redirect(FRONT_SERVER_HTTP.FRONT_SERVER_NAME.'/memberLogin/login/webMessage/replymsg');
			exit;
		}
		$this->pageTitle='送信済メッセージ一覧';
		$condition = "MemberEmailMsg.mailflg='2' and MemberEmailMsg.member_id=".Sanitize::sql($member_id)." and MemberEmailMsg.mail_from='1' " 
						." and MemberEmailMsg.sr_deleted is null and MemberEmailMsg.sr_member_deleted is null"
						." and MemberEmailMsg.msg_deleted is null and MemberEmailMsg.msg_member_deleted is null";
		//返回记录总数
		$count=$this->MemberEmailMsg->findCount($condition);
		//状态排序
		if (isset($status)) {
			$scoutMsgInfo = $this->MemberEmailMsg->findAll($condition,null,'MemberEmailMsg.scout_status',$this->show,$this->page);
			$this->setPaging('ajax','/webMessage/replymsg/srstatus',$count,$this->show,null,$this->show);
		} else {
			$scoutMsgInfo = $this->MemberEmailMsg->findAll($condition,null,null,$this->show,$this->page);
			$this->setPaging('ajax','/webMessage/replymsg',$count,$this->show,null,$this->show);
		}
		for ($i=0;$i<count($scoutMsgInfo);$i++) {
			$scoutMsgInfo[$i]['MemberEmailMsg']['received'] = date("Y/m/d H:i",strtotime($scoutMsgInfo[$i]['MemberEmailMsg']['received']));
		}
		$this->pageInfo($count);
		$this->set("scoutMsgInfo",$scoutMsgInfo);
		$this->render('replymsg');
	}
	
	function pageInfo($count) {
		$page = $this->page;
		$num = $this->show;
		if ($count > 0) {
			$sumpre = $num*($page-1)+1;
		} else {
			$sumpre = 0;
		}
		if($num*$page > $count) {
			$sumnext = $count;
		} else {
			$sumnext = ($num)*($page);
		}
		$this->set('sumpre',$sumpre);
		$this->set('sumnext',$sumnext);
		$this->set('count',$count);
	}
	
	function msgparse($data,$scoutmailflg,$webmailflg,$member_id) {
		
		/*解析msgParams信息，
		 * 解析得到的3个值分别是mailflg(2为web_msg,1为scout_msg)，scout_msg_info的id和company_account_id,
		*/
		$param = split(',', $data[msgParams]);
		
		if ($param[0] == $scoutmailflg) {
			$scoutId = $param[1];
			//$company_account_id = $param[2];
			$scout_msg_id = $param[3];
			$status = $param[4];
			if ($status == '0') {
				$this->ScoutMsgReactionInfo->upStatus($scout_msg_id);
			}
			$msgDetail = $this->ScoutMsgInfo->getScoutMsgInfo($scout_msg_id);
			$msgDetail['scout_msg_id'] = $scout_msg_id;
			if ($status >= '2') {
				$msgDetail['status'] = $status;
			} 
			$msgDetail['received'] = date("Y/m/d H:i",strtotime($msgDetail['received']));
			$msgDetail['contents'] = str_ireplace("\r\n", "<br>", $msgDetail['contents']);
			$msgDetail['contents'] = str_ireplace("\n", "<br>", $msgDetail['contents']);
			// ADD tsubasa 2012/12/24 start
			if($msgDetail["role"] == "System") {
				$company_id = $msgDetail["company_id"];
				$accountData = $this->CompanyAccount->find("company_id = $company_id");
				$msgDetail["name"] = $accountData["CompanyAccount"]["name"];
			}
			// ADD tsubasa 2012/12/24 end
			$this->set("msgDetail",$msgDetail);
			
			if ($msgDetail['status']==0 || $msgDetail['status']==1 || $msgDetail['back']==1) {
				$this->Session->write('reply_flg','1');
			}
			
			$this->render('msgdetail');
		}
		if ($param[0] == $webmailflg) {
			$webId = $param[1];
			$scout_msg_id = $param[2];
			$status = $param[3];
			$srstatus = $param[4];
			$webMsg = $this->WebMsgInfo->find("WebMsgInfo.id=".Sanitize::sql($webId));
			$webMsg['WebMsgInfo']['contents'] = str_replace('%id%'
					,$webMsg['WebMsgInfo']['member_id']
					,$webMsg['WebMsgInfo']['contents']);
			//print_r($webMsg);
			
			if($webMsg['CompanyAccount']['role'] == 'System') {
				// DELETE tsubasa 2012/12/24 start
				// $webMsg['CompanyAccount']['name'] = 'カモメ編集室';
				// DELETE tsubasa 2012/12/24 end
				// ADD tsubasa 2012/12/24 start
				$company_id = $webMsg["Company"]["id"];
				$accountData = $this->CompanyAccount->find("company_id = $company_id");
				$webMsg["CompanyAccount"] = $accountData["CompanyAccount"];
				// ADD tsubasa 2012/12/24 end
			}
			
			
			if(count($param)>5) {
				if($param[5]=='replys') {
					$webMsg['replys']='replys';
				}
			} else {
				if ($status == '0') {
					$this->WebMsgInfo->upStatus($webId);
				}
			}
			
			if ($srstatus == '0' || $srstatus == '1' || $srstatus == '3') {
				$webMsg['resubject'] = '[スカウト]求職者から質問が届いています';
			} 
			$webMsg['WebMsgInfo']['srstatus'] = $srstatus;
			//获取当前信息的状态值，表单提交失败时用于判断单选按钮是否显示
			$webMsg['WebMsgInfo']['nowstatus'] = $srstatus;
			
			$msgDetail = $this->WebMsgInfo->getWebMsgInfo($member_id,$scout_msg_id);
			for ($i=0;$i<count($msgDetail);$i++) {			
				$msgDetail[$i][0]['received']=date("Y/m/d H:i",strtotime($msgDetail[$i][0]['received']));
				$msgDetail[$i][0]['contents'] = str_ireplace("\r\n", "<br>", $msgDetail[$i][0]['contents']);
				$msgDetail[$i][0]['contents'] = str_ireplace("\n", "<br>", $msgDetail[$i][0]['contents']);
			}
			$webMsg['WebMsgInfo']['received'] = date("Y/m/d H:i",strtotime($webMsg['WebMsgInfo']['received']));
			
			$webMsg['WebMsgInfo']['contents'] = str_ireplace("\r\n", "<br>", $webMsg['WebMsgInfo']['contents']);
			$webMsg['WebMsgInfo']['contents'] = str_ireplace("\n", "<br>", $webMsg['WebMsgInfo']['contents']);

			if(!($webMsg['replys']=='replys') 
					&& ($webMsg['WebMsgInfo']['status']!=2 || $webMsg['WebMsgInfo']['back']>=1)) {
				$this->Session->write('reply_flg','1');
			}			
			
			$this->set("webMsg",$webMsg);
			$this->set("msgDetail",$msgDetail);
			$this->render('webmsgdetail');
		}
	}
	
	function msgdetail($back) {
		$member_id = $this->rdAuth->id;
		if (empty($member_id)) {
			$this->redirect(FRONT_SERVER_HTTP.FRONT_SERVER_NAME.'/memberLogin/login/member/remmember');
		}
		//设置后退失效
		$this->setHead();
		
		$this->Session->del('reply_flg');
		
		$this->pageTitle='メッセージ詳細';
		//从application.properties中读取scout的mailflg
		$scoutmailflg = $this->Prop->getValue('config.scoutmailflg');
		//从application.properties中读取web的mailflg
		$webmailflg = $this->Prop->getValue('config.webmailflg');
		if ($back == 'back') {
			$msgDetail = $this->Session->read("msgInfo");
			
			if ($msgDetail['mailflg'] == $scoutmailflg) {
				//判断返回时单选按钮值的设定
// 				$msgDetail['back']='1';
				$this->set("msgDetail",$msgDetail);
				if(!($msgDetail['replys']=='replys') && ($msgDetail['status']!=2 || $msgDetail['back']>=1)) {
					$this->Session->write('reply_flg','1');
				}
				$this->render('msgdetail');
			} 
			if ($msgDetail['mailflg'] == $webmailflg) {
				$webMsgDetail = $this->WebMsgInfo->getWebMsgInfo($member_id,$msgDetail['srid']);
				//lfx added 2013-08-07 start
				for ($i=0;$i<count($webMsgDetail);$i++) {
					$webMsgDetail[$i][0]['received']=date("Y/m/d H:i",strtotime($webMsgDetail[$i][0]['received']));
					$webMsgDetail[$i][0]['contents'] = str_ireplace("\r\n", "<br>", $webMsgDetail[$i][0]['contents']);
					$webMsgDetail[$i][0]['contents'] = str_ireplace("\n", "<br>", $webMsgDetail[$i][0]['contents']);
				}
				//lfx added 2013-08-07 end
				if($msgDetail['CompanyAccount']['role'] == 'System') {
					$msgDetail['CompanyAccount']['name'] = 'カモメ編集室';
				}
				
				if(!($msgDetail['replys']=='replys') && ($msgDetail['WebMsgInfo']['status']!=2 || $msgDetail['WebMsgInfo']['back']>=1)) {
					$this->Session->write('reply_flg','1');
				}
				
				$this->set("webMsg",$msgDetail);
				$this->set("msgDetail",$webMsgDetail);
				$this->render('webmsgdetail');
			}
		} else {
			//获取隐藏域的msgParams信息
			$data = $this->data;
			$this->msgparse($data,$scoutmailflg,$webmailflg,$member_id);
		}		
		exit;
	}
	
	/**
	 * 后退页面处理
	 * */
	private function setHead() {
		header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
		header("Last-Modified:".gmdate("D, d M Y H:i:s") . " GMT");
		header("Cache-Control: no-cache, must-revalidate");
		header("Pragma: no-cache");
	}
	
	function ajaxmsg($scoutId) {
		$this->layout = 'ajax';
		$member_id = $this->rdAuth->id;
		$msgDetail = $this->WebMsgInfo->getAllWebMsgInfo($member_id,$scoutId);
		for ($i=0;$i<count($msgDetail);$i++) {
			$msgDetail[$i][0]['received']=date("Y/m/d H:i",strtotime($msgDetail[$i][0]['received']));
		}
		$this->set("msgDetail",$msgDetail);
	}
	function showmsg($webId) {
		$this->layout = 'ajax';
		
		//从application.properties中读取scout的mailflg
		$scoutmailflg = $this->Prop->getValue('config.scoutmailflg');
		//从application.properties中读取web的mailflg
		$webmailflg = $this->Prop->getValue('config.webmailflg');
		$param = split(',', $webId);
		if ($param[1] == $webmailflg) {
			$webMsg = $this->WebMsgInfo->find("WebMsgInfo.id=".Sanitize::sql($param[0]));
			$webMsg['WebMsgInfo']['received'] = date("Y/m/d H:i",strtotime($webMsg['WebMsgInfo']['received']));
		} 
		if ($param[1] == $scoutmailflg) {
			$webMsg = array();
			$scoutMsg = $this->ScoutMsgInfo->getScoutMsgInfo($param[0]);
			$webMsg['WebMsgInfo']['subject'] = $scoutMsg['subject'];
			$webMsg['WebMsgInfo']['contents'] = $scoutMsg['contents'];
		}
		$webMsg['WebMsgInfo']['contents'] = str_ireplace("\r\n", "<br>", $webMsg['WebMsgInfo']['contents']);
		$webMsg['WebMsgInfo']['contents'] = str_ireplace("\n", "<br>", $webMsg['WebMsgInfo']['contents']);
		$webMsg['WebMsgInfo']['contents'] = str_ireplace("%id%", $webMsg['WebMsgInfo']['member_id'], $webMsg['WebMsgInfo']['contents']);
		$this->set("webMsg",$webMsg);
	}
	
	function returnMsg() {
		// wang.wei add 2012/5/23 start
		$this->Output->htmlspecialchars($this->data);
		// wang.wei add 2012/5/23 end
		$member_id = $this->rdAuth->id;
		if (empty($member_id)) {
			$this->redirect('/memberLogin/webMessage/returnMsg');
			exit;
		}
		
		if (!$this->Session->check('reply_flg')){
			$this->redirect('/webMessage/receivemsg');
			exit;
		}
		
		//设置后退失效
		$this->setHead();
		
		$this->pageTitle='メッセージ返信確認';
		$this->Session->del("msgInfo");
		$data = $this->data;
		$message = '';
		//从application.properties中读取scout的mailflg
		$scoutmailflg = $this->Prop->getValue('config.scoutmailflg');
		//从application.properties中读取web的mailflg
		$webmailflg = $this->Prop->getValue('config.webmailflg');
		/*解析msgParams信息，
		 * 解析得到的3个值分别是mailflg(2为web_msg,1为scout_msg)，scout_msg_info的id和company_account_id,
		*/
		$param = split(',', $data['idAndFlgInfo']);
		if ($param[0] == $scoutmailflg) {
			$scoutMsgId = $data['scout_msg_id'];
			$msgInfo = $this->ScoutMsgInfo->getScoutMsgInfo($scoutMsgId);
			$msgInfo['received'] = date("Y/m/d H:i",strtotime($msgInfo['received']));
			
			//$company_account_id = $param[2];			
			//scout_mail_job_mst表数据插入字段获取
			$companyAccount = $this->CompanyAccount->find("\"CompanyAccount\".id=".Sanitize::sql($msgInfo['company_account_id']));
			$msgInfo['company_accout_email'] = $companyAccount['CompanyAccount']['email'];
			$msgInfo['company_accout_name'] = $companyAccount['CompanyAccount']['name'];
			//valid
			$message = $this->WebMsgInfo->msgValid($this->Valid, $this->data);
			
			if($data['status']=='2') {
				$msgInfo['statusName'] = '[スカウト]興味あり';
			} 
			if ($data['status']=='3') {
				$msgInfo['statusName'] = '[スカウト]求職者から質問が届いています';
			} 
			if ($data['status']=='4') {
				$msgInfo['statusName'] = '[スカウト]辞退する';
			} 
			$msgInfo['status'] = $data['status'];
			$msgInfo['resubject'] = $msgInfo['statusName'];
			$msgInfo['content'] = $msgInfo['contents'];
			$msgInfo['recontent'] = $data['WebMsgInfo']['contents'];
			$msgInfo['scout_msg_id'] = $data['scout_msg_id'];
			$msgInfo['mailflg'] = $scoutmailflg;
			// ADD tsubasa 2012/12/24 start
			if($msgInfo["role"] == "System") {
				$company_id = $msgInfo["company_id"];
				$accountData = $this->CompanyAccount->find("company_id = $company_id");
				$msgInfo["name"] = $accountData["CompanyAccount"]["name"];				
				$msgInfo["CompanyAccount"] = $accountData["CompanyAccount"];				
			}
			// ADD tsubasa 2012/12/24 end
			//判断返回时单选按钮值的设定
			$msgInfo['back']='1';
			if (!empty($message)) {
				$this->set("msgDetail",$msgInfo);
				$this->set("message", $message);
				
				$this->set("htmlValid",implode(',', $this->WebMsgInfo->htmlValid));
				$this->render('msgdetail');
				exit;
			}
		}
		if ($param[0] == $webmailflg) {
			$webId = $param[1];
			$scout_msg_id = $param[2];
			$msgInfo = $this->WebMsgInfo->find("WebMsgInfo.id=".Sanitize::sql($webId));
			$msgInfo['WebMsgInfo']['received'] = date("Y/m/d H:i",strtotime($msgInfo['WebMsgInfo']['received']));
			//valid
			$message = $this->WebMsgInfo->msgValid($this->Valid, $this->data);

			if($data['status']=='2') {
				$msgInfo['statusName'] = '[スカウト]興味あり';
			}
			if ($data['status']=='3') {
				$msgInfo['statusName'] = '[スカウト]求職者から質問が届いています';
			}
			if ($data['status']=='4') {
				$msgInfo['statusName'] = '[スカウト]辞退する';
			}
			if($data['status']>='2' ) {
				$msgInfo['resubject'] = $msgInfo['statusName'];
			} else {
				$msgInfo['resubject'] = $data['WebMsgInfo']['subject'];
			}
			if ($data['nowstatus']=='2' || $data['nowstatus']=='4') {
				$msgInfo['WebMsgInfo']['srstatus'] = $data['nowstatus'];
				$msgInfo['WebMsgInfo']['nowstatus'] = $data['nowstatus'];
			} else {
				$msgInfo['WebMsgInfo']['srstatus'] = $data['status'];
			}
			$msgInfo['content'] = $msgInfo['WebMsgInfo']['contents'];
			$msgInfo['recontent'] = $data['WebMsgInfo']['contents'];
			$msgInfo['srid'] = $scout_msg_id;
			$msgInfo['mailflg'] = $webmailflg;
			// ADD tsubasa 2012/12/24 start
			if($msgInfo["CompanyAccount"]["role"] == "System") {
				$company_id = $msgInfo["Company"]["id"];
				$accountData = $this->CompanyAccount->find("company_id = $company_id");
				$msgInfo["Company_Account"] = $accountData["CompanyAccount"];
			}
			// ADD tsubasa 2012/12/24 end
			//判断返回时单选按钮值的设定
			if ($data['nowstatus']=='2' || $data['nowstatus']=='4') {
				$msgInfo['WebMsgInfo']['back']='2';
			} else {
				$msgInfo['WebMsgInfo']['back']='1';
			}
			$msgDetail['scout_msg_id'] = $scout_msg_id;
			$msgInfo['scout_msg_id'] = $scout_msg_id;
			if (!empty($message)) {
				$msgDetail = $this->WebMsgInfo->getWebMsgInfo($member_id,$scout_msg_id);
				for ($i=0;$i<count($msgDetail);$i++) {
					$msgDetail[$i][0]['received']=date("Y/m/d H:i",strtotime($msgDetail[$i][0]['received']));
				}
				$this->set("webMsg",$msgInfo);
				$this->set("msgDetail",$msgDetail);
				$this->set("message", $message);
				
				$this->set("htmlValid",implode(',', $this->WebMsgInfo->htmlValid));
				$this->render('webmsgdetail');
				exit;
			}
		}
		$this->Session->write("msgInfo", $msgInfo);
		$this->set("msgInfo",$msgInfo);
		$this->render('msgconfirm');
	}

	function saveScoutMail($msgInfo,$scoutmailflg,$webmailflg) {
		$rs = array();
		//lfx modified 2013-09-12 start
		//$rs['mail_from'] = 'web@localhost:82';
		$rs['mail_from'] = 'web@kamome.asia';
		//lfx modified 2013-09-12 end
		$rs['mail_to'] = '';
		$rs['mail_reply_to'] = 'info@localhost:82';
		$rs['mail_cc'] = '';
		$rs['charset'] = '';
		$rs['remain'] = '';
		$rs['mail_status'] = '';
		$rs['scout_msg_id'] = $msgInfo['scout_msg_id'];
		if ($msgInfo['mailflg'] == $scoutmailflg) {			
			$rs['scout_id'] = $msgInfo['id'];			
			$status = $msgInfo['status'];
			if($msgInfo["role"] == "System") {
				$data['name']=$msgInfo['company_name'].'(ID:'.$msgInfo['company_id'].')';
				$rs['mail_to'] = $msgInfo['company_accout_email'].','.$msgInfo['CompanyAccount']['email'];
			} else {
				$data['name']=$msgInfo['company_accout_name'];
				$rs['mail_to'] = $msgInfo['company_accout_email'];
			}			
			
			$data['url']='http://' . ADMIN_SERVER_NAME ."/companyMessage/receivemsg";
			if ($status == '2') {
				$rs['subject']=$this->Prop->getValue('config.scout_mail_title.scout_ok');
				$rs['contents']=$this->getMailTemplate("scout_ok",$data);
			}
			if ($status == '3') {
				$rs['subject']=$this->Prop->getValue('config.scout_mail_title.scout_question');
				$rs['contents']=$this->getMailTemplate("scout_question",$data);
			}
			if ($status == '4') {
				$rs['subject']=$this->Prop->getValue('config.scout_mail_title.scout_no');
				$rs['contents']=$this->getMailTemplate("scout_no",$data);
			}
		}
		if ($msgInfo['mailflg'] == $webmailflg) {			
			$rs['scout_id'] = $msgInfo['ScoutMsgReactionInfo']['scout_id'];			
			$status = $msgInfo['WebMsgInfo']['srstatus'];
			if($msgInfo["CompanyAccount"]["role"] == "System") {
				$data['name']=$msgInfo['Company']['company_name'].'(ID:'.$msgInfo['Company']['id'].')';
				$rs['mail_to'] = $msgInfo['CompanyAccount']['email'].','.$msgInfo['Company_Account']['email'];
			} else {
				$data['name']=$msgInfo['CompanyAccount']['name'];
				$rs['mail_to'] = $msgInfo['CompanyAccount']['email'];
			}			
			$data['url']='http://' . ADMIN_SERVER_NAME ."/companyMessage/receivemsg";
// 			$data['server_name']=SERVER_NAME;
			if ($status == '2') {
				//興味あり
				$rs['subject']=$this->Prop->getValue('config.scout_mail_title.scout_ok');
				$rs['contents']=$this->getMailTemplate("scout_ok",$data);
			}else if ($status == '3') {
				//質問
				$rs['subject']=$this->Prop->getValue('config.scout_mail_title.scout_question');
				$rs['contents']=$this->getMailTemplate("scout_question",$data);
			}else if ($status == '4') {
				//辞退
				$rs['subject']=$this->Prop->getValue('config.scout_mail_title.scout_no');
				$rs['contents']=$this->getMailTemplate("scout_no",$data);
			}
			
			if ($msgInfo['WebMsgInfo']['nowstatus'] == '2') {
				//興味あり後のWebMessage			
				$rs['subject']=$this->Prop->getValue('config.scout_mail_title.web_message');
				$rs['contents']=$this->getMailTemplate("web_message",$data);
			}		
		}
		$this->ScoutMailJob->save($rs);
	}
	
	function confirmMsg() {
		$member_id = $this->rdAuth->id;
		if (empty($member_id)) {
			$this->redirect(FRONT_SERVER_HTTP.FRONT_SERVER_NAME.'/memberLogin/login/member/remmember');
		}
		$msgInfo = $this->Session->read("msgInfo");
		
		//从application.properties中读取scout的mailflg
		$scoutmailflg = $this->Prop->getValue('config.scoutmailflg');
		//从application.properties中读取web的mailflg
		$webmailflg = $this->Prop->getValue('config.webmailflg');
		$saveArr = array();
		
		if ( !empty ($msgInfo)) {
			if ($msgInfo['mailflg'] == $scoutmailflg) {
				$saveArr['scout_flg'] = '1';
				$saveArr['msg_id'] = $msgInfo['id'];
				$saveArr['mail_from'] = '1';
				$saveArr['status'] = '0';
				$saveArr['scout_status'] = $msgInfo['status'];
				$saveArr['company_id'] = $msgInfo['company_id'];
				$saveArr['received'] = strftime("%Y-%m-%d %X",time());
				$saveArr['subject'] = $msgInfo['resubject'];
				$saveArr['contents'] = $msgInfo['recontent'];
				$saveArr['member_id'] = $member_id;
				$saveArr['scout_msg_id'] = $msgInfo['scout_msg_id'];
				$saveArr['company_account_id'] = $msgInfo['company_account_id'];
				//insert scout_mail_job
				$this->saveScoutMail($msgInfo,$scoutmailflg,$webmailflg);
			}
			$this->WebMsgInfo->save($saveArr);
		}
		if ( !empty ($msgInfo) && !empty ($msgInfo['WebMsgInfo'])) {
			if ($msgInfo['mailflg'] == $webmailflg) {
				$saveArr['scout_flg'] = '1';
				$saveArr['msg_id'] = $msgInfo['WebMsgInfo']['id'];
				$saveArr['mail_from'] = '1';
				$saveArr['status'] = '0';
				if ($msgInfo['WebMsgInfo']['nowstatus']!='2' && $msgInfo['WebMsgInfo']['nowstatus']!='4') {
					$saveArr['scout_status'] = $msgInfo['WebMsgInfo']['srstatus'];
				}
				$saveArr['received'] = strftime("%Y-%m-%d %X",time());
				$saveArr['company_id'] = $msgInfo['WebMsgInfo']['company_id'];
				$saveArr['subject'] = $msgInfo['resubject'];
				$saveArr['contents'] = $msgInfo['recontent'];
				$saveArr['member_id'] = $member_id;
				$saveArr['scout_msg_id'] = $msgInfo['WebMsgInfo']['scout_msg_id'];
				$saveArr['company_account_id'] = $msgInfo['WebMsgInfo']['company_account_id'];
				//insert scout_mail_job
				//if (!($msgInfo['WebMsgInfo']['nowstatus'] == '2' || $msgInfo['WebMsgInfo']['nowstatus']=='4')) {
					$this->saveScoutMail($msgInfo,$scoutmailflg,$webmailflg);
				//}
			}
	// 		$this->WebMsgInfo->saveWebMsg($saveArr);
			$this->WebMsgInfo->save($saveArr);
		}
		if ($msgInfo['mailflg'] == $scoutmailflg) {
			$this->ScoutMsgReactionInfo->upReturnMsg($msgInfo['status'],$msgInfo['scout_msg_id']);
		}
		if ($msgInfo['mailflg'] == $webmailflg) {
			$this->ScoutMsgReactionInfo->upReturnMsg($msgInfo['WebMsgInfo']['srstatus'],$msgInfo['WebMsgInfo']['scout_msg_id']);
			$this->WebMsgInfo->upReturnMsg($msgInfo['WebMsgInfo']['id']);
		}
		$this->Session->del('msgInfo');
		$this->Session->del('reply_flg');
		$this->render('finishmsg');
	}
	
	function delInfo() {
		//获取隐藏域的idAndFlgInfo信息
		$data = $this->data;
		$delete = array ();
		//从application.properties中读取scout的mailflg
		$scoutmailflg = $this->Prop->getValue('config.scoutmailflg');
		//从application.properties中读取web的mailflg
		$webmailflg = $this->Prop->getValue('config.webmailflg');
		if (isset($data[idAndFlgInfo])) {
			/*解析idAndFlgInfo信息，
			 * 解析得到的三个值分别是mailflg(2为web_msg,1为scout_msg)，web_msg_info的id或scout_msg_info的id,
			* 最后一个值为scout_msg_reaction_info的id
			*/
			$delete = split(';', $data[idAndFlgInfo]);
			foreach ($delete as $k => $v) {
				$val = split(',', $v);
				if ($val[0] == $webmailflg) {
					$webId .= $val[1].",";
				} else if ($val[0] == $scoutmailflg) {
					$scoutReactionId .= $val[2].",";
				} else if ($val[0] == '3') {
					$noticeReactionId .= $val[2].",";
				}
			}
			$this->WebMsgInfo->doDelete(substr($webId,0,(strlen($webId)-1)));
			$this->ScoutMsgReactionInfo->doDelete(substr($scoutReactionId,0,(strlen($scoutReactionId)-1)));
			
			//南　エンハンス開発対応 
			$this->NoticeMsgReactionInfo->doDelete(substr($noticeReactionId,0,(strlen($noticeReactionId)-1)));
			
		}
	}
	function delMsg(){
		$this->delInfo();
		$this->redirect('/webMessage/receivemsg');
		exit;
	}
	
	function delReply(){
		$this->delInfo();
		$this->redirect('/webMessage/replymsg');
		exit;
	}
	
}
?>
